#include "RatingGUI.h"
#include "Renderer.h"
#include <sstream>
#include <iostream>

using namespace irr; 
using namespace core;
using namespace gui; 
using namespace video; 
using namespace scene; 
using namespace std;

CRatingGUI::CRatingGUI()
{
	CRenderer*	pRender = CRenderer::Instance();
	ISceneNode*	pSceneNode;
	ITextSceneNode* textNode;

	vector<vector<int>> temp(5);
	for(int i = 0; i < 5; i++) {
		temp[i] = vector<int>(10);
	}

	m_rating.resize(NUMOFREVIEW);
	for(int i = 0; i < NUMOFREVIEW; i++)
	{
		m_rating[i].first = 0;
		m_rating[i].second = 0;
	}

	for(int i = 0; i < NUMOFLABEL; i++)
	{
		vector<IGUIStaticText*> textCol;
		for(int j = 0; j < NUMOFREVIEW; j++)
		{
			stringstream ss;
			stringstream tx;

			pSceneNode = pRender->AddBox(vector3df(-167 - 20 * i, -35 + 6 * j, 0),
										 vector3df(5, 5, 2), 
										 SColor(255, 130 + i * 80, 150, 255 - i * 30));

			textNode = (ITextSceneNode*)pRender->AddText("#", vector3df(-167 - 20 * i, -35 + 6 * j, 0), SColor(255, 255, 255, 255));
			textNode->setVisible(false);

			tx << "t:"<<i << "_" << j;
			textNode->setName(tx.str().c_str());

			pSceneNode->setVisible(false);
			ss << i << "_" << j;
			pSceneNode->setName(ss.str().c_str());

		}
	}

	ISceneNode* divider = pRender->AddBox(vector3df(-215, -40 , 0),
										 vector3df(100, 1, 2), 
										 SColor(255, 130, 150, 255));

	// label
	for(int i = 0; i < NUMOFLABEL; i++)
	{
		stringstream ss;

		ss << (i + 1);
		pRender->AddText(ss.str(), vector3df(-167 - i * 20, -45, 0), SColor(255, 255, 255, 255));

		m_Comments[i] = vector<int>(10);
	}
}

CRatingGUI::~CRatingGUI()
{
}

void CRatingGUI::Reset()
{
	m_nodeMap.clear();
	m_indexInfo.clear();
	for(int i = 0; i < NUMOFREVIEW; i++)
	{
		m_rating[i].first = 0;
		m_rating[i].second = 0;
	}
}

void CRatingGUI::SetRating(int nth, int value, float avgEmotion)
{
	if(nth > NUMOFREVIEW)
		return ;

	m_rating[nth] = make_pair(value, avgEmotion);
}

void CRatingGUI::SetNumComments(int nth, int rating, int numComments) {
	m_Comments[rating-1][nth] = numComments;
}

int CRatingGUI::m_Range(float value)
{
	if(value == 0)
		return 0;
	// 5 255 / 0.1 50
	// y = ax + b
	// 255 = a * 5 + b
	// 50 = a * 0.1 + b
	// 205 = 4.9 a 
	int nResult = (int)(41.8 * abs(value) + 45.8);

	nResult = nResult > 255 ? 255 : nResult;
	nResult = nResult < 50 ? 50 : nResult;

	return nResult;
}

void CRatingGUI::Update()
{
	CRenderer*					pRender = CRenderer::Instance();
	irr::scene::ISceneManager*	pScene = pRender->SceneManager();
	vector<int>					rating;
	SColor						color;

	rating.resize(NUMOFLABEL);

	for(int i = 0; i < NUMOFREVIEW; i++)
	{
		if(m_rating[i].first > 0)
		{
			rating[m_rating[i].first - 1]++;
			m_indexInfo[m_rating[i].first - 1].push_back(make_pair(i, m_rating[i].second));
		}
	}

	for(int i = 0; i < NUMOFLABEL; i++)
	{
		for(int j = 0; j < NUMOFREVIEW; j++)
		{
			ISceneNode*		pSceneNode;
			stringstream	ss;

			ss << i << "_" << j;
			pSceneNode = pScene->getSceneNodeFromName(ss.str().c_str());

			ITextSceneNode*		textSceneNode;
			stringstream tx;

			tx << "t:"<<i << "_" << j;
			textSceneNode = (ITextSceneNode*)pScene->getSceneNodeFromName(tx.str().c_str());

			if(j < rating[i])
			{
				pSceneNode->setVisible(true);
				textSceneNode->setVisible(true);

				vector<pair<int, float> > index = m_indexInfo[i];
				m_nodeMap[ss.str()] = index[j].first;
				if(index[j].second > 0)
					color =  SColor(255, m_Range(index[j].second), 30, 30);
				else if(index[j].second == 0)
					color = SColor(255, 150, 150, 150);
				else 
					color = SColor(255, 30, 30, m_Range(index[j].second));
				pSceneNode->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
				pSceneNode->setMaterialTexture(0, pRender->AddTextureFromSColor(color, "box", 
											   dimension2d<u32>(32, 32)));

				vector<int> numVec = m_Comments[i];
				int numComments = numVec[j];

				wstringstream ws;
				ws << numComments; 

				textSceneNode->setText(ws.str().c_str());

			}
			else {
				pSceneNode->setVisible(false);
				textSceneNode->setVisible(false);
			}
		}
	}
}

int CRatingGUI::Index(std::string name)
{
	return m_nodeMap[name];
}